<template>
  <div id="index" ref="appRef" class="index_home" :class="{pageisScale:true}">
    <div class="bg">
      <dv-loading v-if="loading">{{ $t('bigScreen.loading') }}</dv-loading>
      <div v-else class="host-body">
        <div class="d-flex jc-center title_wrap">
          <div class="zuojuxing"/><div class="youjuxing"/><div class="guang"/>
          <div class="d-flex jc-center"><div class="title"><span class="title-text" @click="goToIndex">{{systemName}}</span></div></div>
          <div class="timers">{{dateYear}} {{dateWeek}} {{dateDay}}</div>
        </div>
        <el-dropdown class="user-dropdown" trigger="click">
          <span class="el-dropdown-link"><el-icon class="el-icon-user"/>{{name}}<i class="el-icon-caret-bottom"/></span>
          <el-dropdown-menu slot="dropdown"><el-dropdown-item @click.native="logout">{{ $t('bigScreen.logout') }}</el-dropdown-item></el-dropdown-menu>
        </el-dropdown>
        <index></index>
      </div>
    </div>
  </div>
</template>

<script>
import drawMixin from "../../utils/bigScreen/drawMixin";
import { formatTime } from "../../utils/bigScreen/index.js";
import index from './indexs/index.vue'
import { mapGetters } from 'vuex'
import { listUser } from '@/api/iot/tool'
const { name } = mapGetters({ name: 'name' });
export default {
  mixins:[drawMixin],
  components:{index},
  data(){
    return{
      timing:null,
      loading:true,
      dateDay:null,
      dateYear:null,
      dateWeek:null,
      // 移除硬编码的星期数组
      systemName: '智慧物联管理平台', // 默认系统名称
    };
  },
  filters:{numsFilter(msg) {return msg || 0;},},
  computed:{
    ...mapGetters(['name']),
    // 在计算属性中获取星期几的翻译
    weekday() {
      return [
        this.$t('bigScreen.sunday'),
        this.$t('bigScreen.monday'),
        this.$t('bigScreen.tuesday'),
        this.$t('bigScreen.wednesday'),
        this.$t('bigScreen.thursday'),
        this.$t('bigScreen.friday'),
        this.$t('bigScreen.saturday')
      ];
    }
  },
  created(){this.fetchUserSystemName();},
  mounted(){this.timeFn();this.cancelLoading();},
  beforeDestroy(){clearInterval(this.timing);},
  methods: {
    goToIndex(){this.$router.push({ path: "/index" });},
    showSetting(){this.$refs.setting.init()},
    timeFn() {
      this.timing=setInterval(()=>{
        this.dateDay=formatTime(new Date(),"HH:mm:ss");
        this.dateYear=formatTime(new Date(),"yyyy-MM-dd");
        this.dateWeek=this.weekday[new Date().getDay()];
      },1000);
    },
    cancelLoading() {setTimeout(() => {this.loading = false;}, 500);},
    async logout() {
      this.$confirm(
        this.$t('bigScreen.logoutConfirm'),
        this.$t('bigScreen.tip'),
        {
          confirmButtonText: this.$t('bigScreen.confirm'),
          cancelButtonText: this.$t('bigScreen.cancel'),
          type:'warning'
        }).then(()=>{
        this.$store.dispatch('LogOut').then(()=>{location.href='/index';})
      }).catch(()=>{});
    },
    async fetchUserSystemName() {
      try {
        const userId = this.$store.state.user.userId.toString(); // 获取当前用户ID
        const response = await listUser({ userId });
        if (response.code === 200 && response.rows && response.rows.length > 0) {
          const currentUser = response.rows[0];
          if (currentUser.systemName) {this.systemName = currentUser.systemName;}
        }
      } catch (error) {console.error('获取用户系统名称失败', error);}
    }
  },
};
</script>

<style lang="scss">
@import "./home.scss";@import '../../assets/bigScreen/css/theme/index.css';@import "../../assets/bigScreen/css/index.scss";
.user-dropdown {
  position: absolute;
  top: 5.3%; // 调整到右上角
  right: 2%; // 调整到右上角
  // 增加违和感的样式
  //background-color: rgba(255, 255, 255, 0.7); // 半透明背景色
  border: 1px solid #ddd; // 添加边框
  border-radius: 4px; // 圆角边框
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); // 添加阴影
  transform: translateY(-50%); // 垂直居中
  cursor: pointer; // 鼠标悬停时变为小手形状
  transition: all 0.3s ease; // 过渡效果
  font-size: 21px;
  // 鼠标悬停时的样式
  &:hover {
    background-color: rgba(255, 255, 255, 0.9); // 悬停时背景色加深
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2); // 悬停时阴影加深
  }
}
</style>
